<?php

/**
 * Accesses the tgp_feeds table in the thegridpaper database
 * @author panosbaroudjian
 *
 */
class Application_Model_DbTable_Feeds extends Zend_Db_Table_Abstract
{
    protected $_name = 'tgp_feeds';
    protected $_primary = 'feed_id';
    protected $_dependentTables = array('Application_Model_DbTable_AccountFeeds',
                                        'Application_Model_DbTable_FeedEntries');

    /**
     * Gets a feed by its primary key
     * @param int $id The id of the feed
     * @return NULL|array: An array containing the feed's row if found, NULL otherwise.
     */
    public function getById($id)
    {
        $id = (int)$id;
        $row = $this->find((int)$id);
        if (!$row)
        {
            return null;
        }
        return $row->current()->toArray();
    }

    /**
     * Gets a feed by its unique RSS ID
     * @param string $rss_id The unique RSS ID
     * @return NULL|array: An array containing the feed's row if found, NULL otherwise.
     */
    public function getByRssId($rss_id)
    {
        $select = $this->select()->where('rss_id = ?', $rss_id);
        $row = $this->fetchRow($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Gets a feed by its title
     * @param string $title The title of the feed
     * @return NULL|array: An array containing the feed's row if found, NULL otherwise.
     */
    public function getByTitle($title)
    {
        $select = $this->select()->where('title = ?', $title);
        $row = $this->fetchRow($select);
        if (!$row)
        {
            return null;
        }
        return $row->toArray();
    }

    /**
     * Inserts a new feed
     * @param string $rss_id The unique RSS ID of the feed
     * @param string $title The title of the feed
     * @param string $description The description of the feed
     * @param string $language The language of the feed
     * @param string $url The url of the feed
     * @param int $date The publish date of the feed as a timestamp
     * @return int: The id of the newly inserted feed
     */
    public function insertFeed($rss_id, $title, $description, $language, $url, $date)
    {
        $data = array('rss_id' => $rss_id,
                    'title' => $title,
                    'description' => $description,
                    'language' => $language,
                    'url' => $url,
                    'date_modified' => $date);
        return $this->insert($data);
    }

    /**
     * Updates a feed
     * @param int $id The id of the feed
     * @param string $rss_id The unique RSS ID of the feed
     * @param string $title The title of the feed
     * @param string $description The description of the feed
     * @param string $language The language of the feed
     * @param string $url The url of the feed
     * @param int $date The publish date of the feed as a timestamp
     */
    public function updateFeed($id, $rss_id, $title, $description, $language, $url, $date)
    {
        $where = $this->getAdapter()->quoteInto('feed_id = ?', (int)$id);
        $data = array('rss_id' => $rss_id,
                    'title' => $title,
                    'description' => $description,
                    'language' => $language,
                    'url' => $url,
                    'date_modified' => $date);
        $this->insert($data);
    }

    /**
     * Updates the publish time of a feed
     * @param int $id The id of the feed
     * @param int $date The new publish time as a timestamp
     */
    public function updateDateModified($id, $date)
    {
        $where = $this->getAdapter()->quoteInto('feed_id = ?', $id);
        $data = array('date_modified' => $date);
        $this->update($data, $where);
    }

    /**
     * Deletes a feed
     * @param int $id The id of the feed
     */
    public function deleteFeed($id)
    {
        $where = $this->getAdapter()->quoteInto('feed_id = ?', (int)$id);
        $this->delete($where);
    }
}

